package com.yunwaikeji.gy_tool_library.compress.picture;

import android.graphics.Bitmap;

import com.yunwaikeji.gy_tool_library.base.GYBaseUtils;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class GYCompressPictureThread extends Thread {
    private final GYCompressPictureBuilder bitmapCompressBuilder;
    private final GYCompressPictureCallback GYCompressPictureCallback;

    public GYCompressPictureThread(GYCompressPictureBuilder bitmapCompressBuilder, GYCompressPictureCallback GYCompressPictureCallback) {
        this.bitmapCompressBuilder = bitmapCompressBuilder;
        this.GYCompressPictureCallback = GYCompressPictureCallback;
    }

    @Override
    public void run() {
        super.run();
        Bitmap bitmap;
        if (bitmapCompressBuilder.getMaxWidth() > 0 && bitmapCompressBuilder.getMaxHeight() > 0) {
            bitmap = GYDisplayUtils.getBitmap(bitmapCompressBuilder.getPath(), bitmapCompressBuilder.getMaxWidth(), bitmapCompressBuilder.getMaxHeight());
        } else {
            bitmap = GYDisplayUtils.getBitmap(bitmapCompressBuilder.getPath());
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int quality = bitmapCompressBuilder.getQuality();
        if (quality < 1) {
            quality = 1;
        }
        if (quality > 100) {
            quality = 100;
        }
        //压缩格式选取JPEG就行了，quality，压缩精度尽量不要低于50，否则影响清晰度
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);

        long compressSize = bitmapCompressBuilder.getCompressSize();
        if (compressSize >= 0) {
            while (quality > 1 && compressSize < byteArrayOutputStream.toByteArray().length) {
                quality--;
                byteArrayOutputStream.reset();
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
            }
        }
        try {
            final File compressPath = new File(bitmapCompressBuilder.getCompressPath());
            FileOutputStream fileOutputStream = new FileOutputStream(compressPath);
            fileOutputStream.write(byteArrayOutputStream.toByteArray());//写入目标文件
            fileOutputStream.flush();
            fileOutputStream.close();
            byteArrayOutputStream.close();
            if (compressPath.length() > 0)
                if (GYCompressPictureCallback != null) {
                    GYBaseUtils.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //压缩成功
                            GYCompressPictureCallback.success(compressPath, bitmap);
                        }
                    });
                }
        } catch (final Exception e) {
            e.printStackTrace();
            if (GYCompressPictureCallback != null) {
                GYBaseUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //压缩失败
                        GYCompressPictureCallback.error("压缩图片文件失败" + e.getMessage());
                    }
                });
            }
        }
    }
}
